class Test extends Thread {
    static private int t = 20;

    public Test(String name) {
        super(name);
    }

    public synchronized boolean tickets(int m) {
        if (t > 0) {
            System.out.printf("%s 出售一张票 %d, 时间 %d 毫秒\n", this.getName(), t--, m);
        } else {
            return true;
        }
        return false;
    }

    public void run() {
        int m = (int)(Math.random() * 900 + 100);
        while (true) {
            try {
                this.sleep(m);
            } catch (Exception e) {}
            if (tickets(m))
                break;
        }
    }
}

public class ThreadTest {
    static public void main(String [] args) {
        (new Test("售票口1")).start();
        (new Test("售票口2")).start();
        (new Test("售票口3")).start();
        (new Test("售票口4")).start();

        /*
         * int s = 1;
         * while (s < 100) {
         *     System.out.printf("Main run %d\n", s++);
         * }
         */
    }
}
